class Zcsh {
    public int deleteAndEarn(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        } else if (nums.length == 1) {
            return nums[0];
        }

        int maxValue = nums[0];
        for (int value : nums) {
            maxValue = Math.max(maxValue, value);
        }

        int[] all = new int[maxValue + 1];
        for (int i = 0; i < nums.length; i++) {
            all[nums[i]]++;
        }
        
        int result = 0;
        for (int i = 2; i < all.length; i++) {
            int select = all[i - 2]  + (all[i] * i);
            int notSelect = all[i - 1];
            all[i] = Math.max(select, notSelect);
            result = Math.max(all[i], result);
        }
        return result;
    }
}